<!--
 * @Author: your name
 * @Date: 2022-03-14 18:51:21
 * @LastEditTime: 2022-03-14 19:27:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \geyao\geyao\数组方法splice220314.html
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组方法splice</title>
</head>
<body>
  <script>
    //splice(start,end,index1,index2) 从什么位置开始，删几个？删了后插入什么新元素？
    var geyao=["1","2","3","4"]
    var geyaotest=["1","2","3","4"]
    var geyaofangfang=["1","2","3","4"]
    var fangfang=["1","2","3","4"]
    var fangfangTest=["1","2","3","4"]
    var fangfangBang=["1","2","3","4"]
    var kangkang=["1","2","3","4"]
    var kangkangni=["1","2","3","4"]
    var kangkanghao=["1","2","3","4"]
    //1个参数会截取当前 大于2之后的数都会被删除
    geyao.splice(2)
    console.log(geyao,"geyao")//["1","2"]
    //2个参数 表示从什么位置截取到什么位置 把下标为2的截取掉
    geyaotest.splice(2,1)
    console.log(geyaotest,"geyaotest") //[ "1", "2", "4" ]
    //3个参数 在什么位置到什么位置添加
    geyaofangfang.splice(2,1,"5")
    console.log(geyaofangfang,"geyaofangfang") //[ "1", "2", "5", "4" ]
    //超出数组长度 默认获得前面的全部 
    fangfang.splice(5)
    console.log(fangfang,"fangfang") // [ "1", "2", "3", "4" ]
    //从5之后开始截取
    fangfangTest.splice(5,1)
    console.log(fangfangTest,"fangfangTest") // [ "1", "2", "3", "4" ]
    //对后置的位置进行添加
    fangfangBang.splice(5,1,"6")
    console.log(fangfangBang,"fangfangBang") // [ "1", "2", "3", "4","6"]
    //倒着开始截取剩余部分
    kangkang.splice(-1)
    console.log(kangkang,"kangkang") // [ "1", "2", "3" ]
    //倒着进行处理
    kangkangni.splice(-2,1)
    console.log(kangkangni,"kangkangni")//[ "1", "2", "4" ]
    //倒着插入对应值
    kangkanghao.splice(-2,1,"8")
    console.log(kangkanghao,"kangkanghao")//[ "1", "2", "8", "4" ]
    //该方法向或者从数组中添加或者删除项目，返回被删除的项目。（该方法会改变原数组）
  </script>
</body>
</html>